package com.lixiaofeng.paintstudy;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 随机生成四位验证码的文本控件
 * Created by lanouhn on 16/8/18.
 */
public class RandomTextView extends TextView {
    private String mTitleText;//文本
    private int mTitleTextColors;//颜色
    private int mTitleTextSize;//大小

    private Paint mPaint;//画笔

    private Rect mBound;//绘制文本时控制文本的绘制范围

    public RandomTextView(Context context) {
        this(context, null);
    }

    public RandomTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 获取自定义组件的样式属性
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */

    public RandomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获得我们所有的自定义属性的样式值
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RandomTextView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.RandomTextView_titleText:
                    mTitleText = array.getString(attr);
                    break;
                case R.styleable.RandomTextView_titleTextSize:
                    //如果没有传入大小，那么默认值是16
                    mTitleTextSize = array.getDimensionPixelSize(attr,
                            (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()))
                    );
                    break;
                case R.styleable.RandomTextView_titleTextColors:
                    //如果没有传入颜色值，那么默认为黑色
                    mTitleTextColors = array.getInt(attr, Color.BLACK);
                    break;
            }


        }
        array.recycle();

        /**
         * 获取绘制文本的宽和高
         */
        mPaint = new Paint();
        mPaint.setTextSize(mTitleTextSize);
        mPaint.setColor(mTitleTextColors);

        mBound = new Rect();
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);


        //定义控件的点击事件
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTitleText = getRandomText();
                postInvalidate();//刷新页面
            }
        });
    }

    /**
     * 随机生成一个四位随机数
     *
     * @return
     */
    private String getRandomText() {
        Random random = new Random();
        Set<Integer> set = new HashSet<>();
        while (set.size() < 4) {
            int randomInt = random.nextInt(10);
            set.add(randomInt);
        }
        StringBuffer buffer = new StringBuffer();
        for (Integer i : set) {
            buffer.append("" + i);
        }

        return buffer.toString();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width, height;

        //得到控件的真实宽度
        if (widthMode == MeasureSpec.EXACTLY) {
            //指定了具体的宽高，或者Match_parent
            width = widthSize;
        } else {
            //wrap_content
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textWidth = mBound.width();
            width = (int) (getPaddingLeft() + textWidth + getPaddingRight());
        }

        //得到控件的真实高度
        if (heightMode == MeasureSpec.EXACTLY) {
            //指定了具体的宽高，或者Match_parent
            height = heightSize;
        } else {
            //wrap_content
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textHeight = mBound.height();
            height = (int) (getPaddingTop() + textHeight + getPaddingBottom());
        }

        //设置控件的宽高
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        mPaint.setColor(Color.RED);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);


    }


}
